package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(path = "/member")
public class MemberController {


    @Reference
    private MemberService memberService;

    /**
     * 会员档案列表查询
     */

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try {
            PageResult pageResult = memberService.pageQuery(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, pageResult);
        } catch (Exception e) {
            return new Result(true, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Member> list= memberService.findAll();
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    //添加会员
    @PostMapping("/add")
    public Result add(@RequestBody Member member) {
        try {
            member.setRegTime(new Date());
            member.setStatus(0);
            memberService.add(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    //查询单个会员信息
    @GetMapping("{id}")
    public Result findById(@PathVariable("id") int memberid ){
        try {
          Member member =  memberService.findById(memberid);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    //更新会员信息
    @PutMapping("/update")
    public Result update(@RequestBody Member member){
        try {
            memberService.update(member);
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    /**
     * 修改启用禁用状态
     */
    @PutMapping("{id}/{status}")
    public Result status(@PathVariable("id") int memberid,@PathVariable("status") int statusId ){
        try {
            Member member = memberService.findById(memberid);
            member.setStatus(statusId);
            update(member);
            return new Result(true,MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_MEMBER_STATUS_FAIL);
        }

    }


}
